using DG.Tweening;
using Spine.Unity;
using UnityEngine;

public class DogGetCoinController : MonoBehaviour
{
	public GameObject Player;

	private float distance;

	private float distance1;

	private bool isGetCoin;

	private GameObject _target;

	private GameObject[] Coin;

	private int increaseCoin;

	private Tweener _tweenMove;

	private bool isMove;

	private void Start()
	{
		Coin = GameObject.FindGameObjectsWithTag("Coin");
		if (GameManager.stage <= 20)
		{
			increaseCoin = 15;
		}
		else if (GameManager.stage > 20 && GameManager.stage <= 30)
		{
			increaseCoin = 25;
		}
		else if (GameManager.stage > 30 && GameManager.stage <= 40)
		{
			increaseCoin = 30;
		}
		else if (GameManager.stage > 40 && GameManager.stage <= 50)
		{
			increaseCoin = 50;
		}
		else
		{
			increaseCoin = 80;
		}
	}

	private void Update()
	{
		if (!isGetCoin)
		{
			Vector3 position = base.transform.position;
			float x = position.x;
			Vector3 position2 = Player.transform.position;
			distance = Mathf.Abs(x - position2.x);
			if (distance > 1f)
			{
				_tweenMove.Kill();
				isMove = false;
				Vector3 position3 = base.transform.position;
				float x2 = position3.x;
				Vector3 position4 = Player.transform.position;
				if (x2 < position4.x)
				{
					base.transform.Translate(Vector2.right * 2f * Time.deltaTime);
					GetComponent<SkeletonAnimation>().skeleton.FlipX = true;
					GetComponent<SkeletonAnimation>().AnimationName = "walk2";
				}
				else
				{
					base.transform.Translate(-Vector2.right * 2f * Time.deltaTime);
					GetComponent<SkeletonAnimation>().skeleton.FlipX = false;
					GetComponent<SkeletonAnimation>().AnimationName = "walk2";
				}
			}
			else
			{
				GetComponent<SkeletonAnimation>().AnimationName = "walk";
				if (!isMove)
				{
					Vector3 position5 = base.transform.position;
					float x3 = position5.x;
					Vector3 position6 = Player.transform.position;
					if (x3 < position6.x)
					{
						Transform transform = base.transform;
						Vector3 position7 = Player.transform.position;
						_tweenMove = transform.DOMoveX(position7.x + 1f, 1.5f).SetEase(Ease.Linear).OnComplete(FlipMove);
						GetComponent<SkeletonAnimation>().skeleton.FlipX = true;
					}
					else
					{
						Transform transform2 = base.transform;
						Vector3 position8 = Player.transform.position;
						_tweenMove = transform2.DOMoveX(position8.x - 1f, 1.5f).SetEase(Ease.Linear).OnComplete(FlipMove);
						GetComponent<SkeletonAnimation>().skeleton.FlipX = false;
					}
					isMove = true;
				}
			}
		}
		if (_target == null)
		{
			FindTarget();
			isGetCoin = false;
		}
		else
		{
			Vector3 position9 = base.transform.position;
			float x4 = position9.x;
			Vector3 position10 = _target.transform.position;
			distance1 = Mathf.Abs(x4 - position10.x);
			isGetCoin = true;
			_tweenMove.Kill();
			isMove = false;
		}
		if (isGetCoin)
		{
			Vector3 position11 = base.transform.position;
			float x5 = position11.x;
			Vector3 position12 = _target.transform.position;
			if (x5 < position12.x)
			{
				GetComponent<SkeletonAnimation>().skeleton.FlipX = true;
			}
			else
			{
				GetComponent<SkeletonAnimation>().skeleton.FlipX = false;
			}
			base.transform.position = Vector3.MoveTowards(base.transform.position, _target.transform.position, 2f * Time.deltaTime);
			GetComponent<SkeletonAnimation>().AnimationName = "run";
		}
	}

	private void FlipMove()
	{
		Vector3 position = base.transform.position;
		float x = position.x;
		Vector3 position2 = Player.transform.position;
		if (x < position2.x)
		{
			Transform transform = base.transform;
			Vector3 position3 = Player.transform.position;
			_tweenMove = transform.DOMoveX(position3.x + 1f, 1.5f).SetEase(Ease.Linear).OnComplete(FlipMove);
			GetComponent<SkeletonAnimation>().skeleton.FlipX = true;
		}
		else
		{
			Transform transform2 = base.transform;
			Vector3 position4 = Player.transform.position;
			_tweenMove = transform2.DOMoveX(position4.x - 1f, 1.5f).SetEase(Ease.Linear).OnComplete(FlipMove);
			GetComponent<SkeletonAnimation>().skeleton.FlipX = false;
		}
		isMove = true;
	}

	private void FindTarget()
	{
		Coin = GameObject.FindGameObjectsWithTag("Coin");
		for (int i = 0; i < Coin.Length; i++)
		{
			Vector3 position = base.transform.position;
			float x = position.x;
			Vector3 position2 = Coin[i].transform.position;
			distance1 = Mathf.Abs(x - position2.x);
			if (distance1 < 10f)
			{
				_target = Coin[i];
			}
		}
	}

	private void OnTriggerEnter2D(Collider2D target)
	{
		if (target.gameObject.tag == "Coin")
		{
			AudioController.Instance.SoundPlayerEatItem();
			target.gameObject.GetComponent<GoldController>().isMagnet = true;
			target.gameObject.GetComponent<GoldController>()._value = increaseCoin;
		}
	}
}
